NAME = "Admin Stanalone Multihacks"
VERSION = "0.2"

include("addresses.lua");
include("settings.lua");
include("functions.lua");

setStartKey(hotkeys.START_BOT.key);
setStopKey(hotkeys.STOP_BOT.key);

local HackState = {
	Fly = OFF,
	Wall = OFF,
	Speed = {SetValue = first_high_speed, Enabled = false},
}

function exitCallback()
	printf("\n")
	setupConsole("reset")
end
atExit(exitCallback);

function stopPE()
	printf("\n")
	setupConsole("reset")
	__PErunning = false;
	error("Script forcibly terminated.", 0);
end

function printoutupdate()
	local function namekey(name,value)
		return string.format("|yellow|%s: %s   ",name,value)
	end
	local speed
	if HackState.Speed.Enabled then
		speed = string.format("|lightgreen|%-3d", HackState.Speed.SetValue)
	else
		speed = OFF
	end

	cprintf_ex(namekey("Fly",HackState.Fly)..
			   namekey("Wall",HackState.Wall)..
			   namekey("Speed",speed)..
			   "|lightgreen|("..getKeyName(hotkeys.HELP.key)..") Help.\r")

end

function showhelp()
	setupConsole(80,10)
	cprintf(cli.lightgreen,"\n\n   Rock5s Multihack v%s\n\n",VERSION)
	cprintf_ex("|yellow|Toggle fly\t|white|%s\n",getKeyName(hotkeys.FLY_TOGGLE.key))
	cprintf_ex("|yellow|Toggle wall\t|white|%s\n",getKeyName(hotkeys.WALL_TOGGLE.key))
	cprintf_ex("|yellow|Toggle speed\t|white|%s\n",getKeyName(hotkeys.SPEED_TOGGLE.key))
	cprintf_ex("|yellow|Speed down\t|white|%s\n",getKeyName(hotkeys.SPEED_DOWN.key))
	cprintf_ex("|yellow|Speed up\t|white|%s\n",getKeyName(hotkeys.SPEED_UP.key))

	cprintf_ex("|lightgreen|'%s' to return\n",getKeyName(hotkeys.HELP.key))

	repeat yrest(50) until keyPressedLocal(hotkeys.HELP.key)
	repeat yrest(50) until not keyPressedLocal(hotkeys.HELP.key)
	printf("\n")
	setupConsole(80,1)
end

local oldW, oldH, oldX, oldY
if getConsoleAttributes then
	oldW, oldH, oldX, oldY = getConsoleAttributes()
end

function setupConsole(w,h)
	if setConsoleAttributes then
		if w == "reset" then
			setConsoleAttributes(oldW,oldH)
		else
			local tmpW,tmpH, tmpX,tmpY = getConsoleAttributes()
			if w > tmpX then
				setConsoleAttributes(tmpW,tmpH,w,tmpY)
			end
			setConsoleAttributes(w,h)
		end
	end
end

function main()
	attach(getWin());

	if( not checkExecutableCompatible() ) then
		printf("Addresses seem off, trying to update.\n")
		include("update.lua")
		if( not checkExecutableCompatible() ) then
			error("The addresses didn't update, you will have to wait until there is an SVN update that updates the addresses.\n")
		else
			cprintf(cli.yellow,"update.lua seems to have worked, addresses.lua has been updated.\n")
		end
	end

	setWindowName(getHwnd(),NAME.." - " .. VERSION)
	setupConsole(80,1)
	if setWindowPos then
		local gameX,gameY = windowRect(getWin())
		setWindowPos(getHwnd(), gameX+position_X, gameY+position_Y,nil, nil, true)
	end
	printoutupdate()

	local hf_key_pressed, hf_key;
	while(true) do

		hf_key_pressed = false;

		if keyComboPressed(hotkeys.HELP) then
			hf_key_pressed = true;
			hf_key = "HELP";
		end;
		if keyComboPressed(hotkeys.FLY_TOGGLE) then
			hf_key_pressed = true;
			hf_key = "FLY_TOGGLE";
		end;
		if keyComboPressed(hotkeys.WALL_TOGGLE) then
			hf_key_pressed = true;
			hf_key = "WALL_TOGGLE";
		end;
		if keyComboPressed(hotkeys.SPEED_TOGGLE) then
			hf_key_pressed = true;
			hf_key = "SPEED_TOGGLE";
		end;
		if keyComboPressed(hotkeys.SPEED_UP) then
			hf_key_pressed = true;
			hf_key = "SPEED_UP";
		end;
		if keyComboPressed(hotkeys.SPEED_DOWN) then
			hf_key_pressed = true;
			hf_key = "SPEED_DOWN";
		end;


		if( hf_key_pressed == false and 	-- key released, do the work
			hf_key ) then					-- and key not empty

			if( hf_key == "HELP" ) then
				showhelp();
			end;
			if( hf_key == "FLY_TOGGLE" ) then
				HackState.Fly = flytoggle();
			end;
			if( hf_key == "WALL_TOGGLE" ) then
				HackState.Wall = walltoggle();
			end;
			if( hf_key == "SPEED_TOGGLE" ) then
				speedtoggle(HackState.Speed.Enabled, HackState.Speed.SetValue)
				HackState.Speed.Enabled = not HackState.Speed.Enabled
			end;
			if( hf_key == "SPEED_UP" ) then
				HackState.Speed.Enabled = true
				HackState.Speed.SetValue = speedup() or HackState.Speed.SetValue
			end;
			if( hf_key == "SPEED_DOWN" ) then
				HackState.Speed.Enabled = true
				HackState.Speed.SetValue = speeddown() or HackState.Speed.SetValue
			end;
			hf_key = nil;	-- clear last pressed key
			printoutupdate()
		end
		yrest(10)
	end

end

startMacro(main,true);
